package com.base.cn.platform.os.service.manage.area;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.dao.manage.area.SysAreaDao;
import com.base.cn.platform.os.entity.manage.area.SysArea;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SysAreaBiz extends BaseBiz<SysArea,SysAreaDao> {


    @Autowired
    SysAreaDao sysAreaDao;
    /**
     * 根据Ids搜索列表
     * @param areaIds
     * @return
     */
    public List<SysArea> querySysAreaListByIds(String areaIds) {
        List<SysArea> sysAreas = new ArrayList<>();
        if (StringUtils.isNotEmpty(areaIds)) {
            sysAreas = this.find( " id in (" + areaIds + ")",null,null);
        }
        return sysAreas;
    }

    /**
     * 根据Ids搜索返回map
     * @param areaIds
     * @return
     */
    public Map<BigDecimal,SysArea> querySysAreaMapByIds(String areaIds){
        if (StringUtils.isNotEmpty(areaIds)){
            List<SysArea> sysAreas = this.querySysAreaListByIds(areaIds);
            Map<BigDecimal,SysArea> sysAreaMap = sysAreas.stream().collect(Collectors.toMap(SysArea::getId,e->e));
            return sysAreaMap;
        }
        return null;
    }

    /**
     * 根据 parentId 搜索返回map
     * @param parentId
     * @return
     */
    public Map<BigDecimal,SysArea> querySysAreaMapByParentId(BigDecimal parentId){
        if (parentId!=null){
            SysArea sysArea = new SysArea();
            sysArea.setParentId(parentId);
            List<SysArea> sysAreas = this.queryUserAreaList(sysArea);
            Map<BigDecimal,SysArea> sysAreaMap = sysAreas.stream().collect(Collectors.toMap(SysArea::getId,e->e));
            return sysAreaMap;
        }
        return null;
    }

    /**
     * 通过父级id串，查询地区列表
     * @param parentIds 父级id串
     * @return
     */
    public Map<BigDecimal,List<SysArea>> querySysAreaMapByParentIds(String parentIds){
        List<SysArea> areaList = this.find(" parentId in ("+parentIds+")",null,null);
        if(ObjectUtils.isNotEmpty(areaList)){
            return areaList.stream().collect(Collectors.groupingBy(e -> e.getParentId()));
        }
        return null;
    }

    /**
     * 多条件查询地区列表
     *
     * @param userArea 查询条件
     * @return List<UserArea>
     */
    public List<SysArea> queryUserAreaList(SysArea userArea) {
        String whereSql = " 1=1";
        if (userArea.getParentId() != null) {
            whereSql += " and parentId=" + userArea.getParentId();
        }
        if (userArea.getAreaType() != null) {
            whereSql += " and areaType = " + userArea.getAreaType();
        }
        List<SysArea> sysAreas =this.find( whereSql,null,null);
        return sysAreas;
    }

    /**
     * 获取完整的地区表
     * @return
     */
    public SysArea queryFullAddress(Integer areaType,String name){
        //获取省级单位
        StringBuilder sql =new StringBuilder(" areaType = ");
        sql.append(areaType);
        sql.append("  and  areaName = '");
        sql.append(name).append("'");
        SysArea area = this.findOne( sql.toString(),null);
        return      area;
    }

}
